//
//  CommonAPI.m
//  ShopProject
//
//  Created by 小焘无量 on 2018/9/27.
//  Copyright © 2018年 ShopProject. All rights reserved.
//

#import "CommonAPI.h"

@implementation CommonAPI

+(BOOL)telPhoneCheck:(NSString *)tel {
    if (tel.length != 11) {
        return NO;
    }
    return YES;
}

+(BOOL)passwordCheck:(NSString *)password {
    if ((password.length < 6) || (password.length > 16)) {
        return NO;
    }
    return YES;
}

+(BOOL)StringIsNull:(NSString *)str {
    if ((str == nil) || (str == NULL)) {
        return YES;
    }
    if ([str isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if (str.length == 0) {
        return YES;
    }
    if ([str isEqualToString:@""]) {
        return YES;
    }
    return NO;
}

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}


+(NSString *)getImageFilePath{
    NSString *dataPath = [NSString stringWithFormat:@"%@/Documents/allImages", NSHomeDirectory()];
    NSFileManager *fm = [NSFileManager defaultManager];
    if(![fm fileExistsAtPath:dataPath]){
        [fm createDirectoryAtPath:dataPath
      withIntermediateDirectories:YES
                       attributes:nil
                            error:nil];
    }
    return dataPath;
    
    
}
+(NSString *)getOnlyId{
    return  [NSString stringWithFormat:@"%d%d",(int)[[NSDate date] timeIntervalSince1970], arc4random() % 100000];
    
    
}

/************
 
 日期格式请传入：2013-08-05 12:12:12；如果修改日期格式，比如：2013-08-05，则将[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];修改为[df setDateFormat:@"yyyy-MM-dd"];
 
 ***********/

+(int)compareDate:(NSString*)date01 withDate:(NSString*)date02{
    
    
    
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    
    [df setDateFormat:@"yyyy-MM-dd"];
    
    
    NSDate *dateA  = [df dateFromString:date01];
    
    NSDate *dateB = [df dateFromString:date02];
    
    
    NSComparisonResult result = [dateA compare:dateB];
    
    if (result == NSOrderedDescending) {
        //NSLog(@"oneDay比 anotherDay时间晚");
        return 1;
    }
    else if (result == NSOrderedAscending){
        //NSLog(@"oneDay比 anotherDay时间早");
        return -1;
    }
    //NSLog(@"两者时间是同一个时间");
    return 0;
    
}
+(NSString *)getCurrentDay{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}
+(NSString *)getCurrentMonth{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}
+(NSString *)getCurrentYear{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return dateTime;
}

+(BOOL)userLoginStatus {
    NSString *loginTel;
    loginTel = [UserDefaults readFromPlist:LOGIN_STATUS_TEL];
    if ([CommonAPI StringIsNull:loginTel]) {
        return NO;
    }
    return YES;
}

@end
